<!--
  ~ Licensed to the Apache Software Foundation (ASF) under one
  ~ or more contributor license agreements.  See the NOTICE file
  ~ distributed with this work for additional information
  ~ regarding copyright ownership.  The ASF licenses this file
  ~ to you under the Apache License, Version 2.0 (the
  ~ "License"); you may not use this file except in compliance
  ~ with the License.  You may obtain a copy of the License at
  ~
  ~   http://www.apache.org/licenses/LICENSE-2.0
  ~
  ~ Unless required by applicable law or agreed to in writing,
  ~ software distributed under the License is distributed on an
  ~ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  ~ KIND, either express or implied.  See the License for the
  ~ specific language governing permissions and limitations
  ~ under the License.
-->

<app-help-message-show
  [help_message_content]="'tag.help' | i18n"
  [guild_link]="'tag.help.link' | i18n"
  [module_name]="'menu.advanced.tags'"
  [icon_name]="'tags'"
></app-help-message-show>

<nz-divider></nz-divider>

<app-toolbar>
  <ng-template #left>
    <button nz-button nzType="primary" nz-tooltip (click)="sync()" [nzTooltipTitle]="'common.refresh' | i18n">
      <i nz-icon nzType="sync" nzTheme="outline"></i>
    </button>
    <button nz-button nzType="primary" (click)="onNewTag()">
      <i nz-icon nzType="appstore-add" nzTheme="outline"></i>
      {{ 'tag.new' | i18n }}
    </button>

    <button nz-button nzType="primary" nzDanger (click)="onDeleteTags()">
      <i nz-icon nzType="delete" nzTheme="outline"></i>
      {{ 'tag.delete' | i18n }}
    </button>
  </ng-template>
  <ng-template #right>
    <app-multi-func-input
      class="mobile-hide"
      groupStyle="width: 250px;"
      [placeholder]="'tag.search' | i18n"
      [(value)]="search"
      (valueChange)="loadTagsTable()"
    />
  </ng-template>
</app-toolbar>

<nz-table
  #fixedTable
  [nzData]="tags"
  [nzPageIndex]="pageIndex"
  [nzPageSize]="pageSize"
  [nzTotal]="total"
  nzFrontPagination="false"
  [nzLoading]="tableLoading"
  nzShowSizeChanger
  [nzShowTotal]="rangeTemplate"
  [nzPageSizeOptions]="[8, 15, 25]"
  (nzQueryParams)="onTablePageChange($event)"
  nzShowPagination="true"
  [nzScroll]="{ x: '1240px' }"
>
  <thead>
    <tr>
      <th nzAlign="center" nzLeft nzWidth="3%" [(nzChecked)]="checkedAll" (nzCheckedChange)="onAllChecked($event)"></th>
      <th nzAlign="center" nzWidth="15%">{{ 'tag' | i18n }}</th>
      <th nzAlign="center" nzWidth="15%">{{ 'tag.name' | i18n }}</th>
      <th nzAlign="center" nzWidth="15%">{{ 'tag.value' | i18n }}</th>
      <th nzAlign="center" nzWidth="15%">{{ 'tag.description' | i18n }}</th>
      <th nzAlign="center" nzWidth="20%">{{ 'tag.update-time' | i18n }}</th>
      <th nzAlign="center" nzWidth="17%">{{ 'common.edit' | i18n }}</th>
    </tr>
  </thead>
  <tbody>
    <tr *ngFor="let data of fixedTable.data">
      <td nzAlign="center" nzLeft [nzChecked]="checkedTagIds.has(data.id)" (nzCheckedChange)="onItemChecked(data.id, $event)"></td>
      <td nzAlign="center">
        <a routerLink="/monitors" [queryParams]="{ tag: formatTagName(data) }">
          <nz-tag [nzColor]="data.color" style="font-weight: bold; font-size: larger" class="hoverClass">
            {{ formatTagName(data) }}
          </nz-tag>
        </a>
      </td>
      <td nzAlign="center">{{ data.name }}</td>
      <td nzAlign="center">{{ data.tagValue }}</td>
      <td nzAlign="center">{{ data.description }}</td>
      <td nzAlign="center">{{ data.gmtUpdate | date : 'YYYY-MM-dd HH:mm:ss' }}</td>
      <td nzAlign="center">
        <div class="actions">
          <button nz-button nzType="primary" (click)="onEditOneTag(data)" nz-tooltip [nzTooltipTitle]="'tag.edit' | i18n">
            <i nz-icon nzType="edit" nzTheme="outline"></i>
          </button>
          <button nz-button nzType="primary" nzDanger (click)="onDeleteOneTag(data.id)" nz-tooltip [nzTooltipTitle]="'tag.delete' | i18n">
            <i nz-icon nzType="delete" nzTheme="outline"></i>
          </button>
        </div>
      </td>
    </tr>
  </tbody>
</nz-table>

<ng-template #rangeTemplate> {{ 'common.total' | i18n }} {{ total }} </ng-template>

<!-- new or update TAG pop-up box -->
<nz-modal
  [(nzVisible)]="isManageModalVisible"
  [nzTitle]="isManageModalAdd ? ('tag.new' | i18n) : ('tag.edit' | i18n)"
  (nzOnCancel)="onManageModalCancel()"
  (nzOnOk)="onManageModalOk()"
  nzMaskClosable="false"
  nzWidth="30%"
  [nzOkLoading]="isManageModalOkLoading"
>
  <div *nzModalContent class="-inner-content">
    <form nz-form #tagForm="ngForm">
      <nz-form-item>
        <nz-form-label [nzSpan]="7" nzFor="name" nzRequired="true">{{ 'tag.name' | i18n }}</nz-form-label>
        <nz-form-control [nzSpan]="12" [nzErrorTip]="'validation.required' | i18n">
          <input [(ngModel)]="tag.name" nz-input required name="name" type="text" id="name" />
        </nz-form-control>
      </nz-form-item>
      <nz-form-item>
        <nz-form-label [nzSpan]="7" nzFor="value">{{ 'tag.value' | i18n }}</nz-form-label>
        <nz-form-control [nzSpan]="12" [nzErrorTip]="'validation.required' | i18n">
          <input [(ngModel)]="tag.tagValue" nz-input name="value" type="text" id="value" />
        </nz-form-control>
      </nz-form-item>
      <nz-form-item>
        <nz-form-label [nzSpan]="7" nzFor="color">{{ 'tag.color' | i18n }}</nz-form-label>
        <nz-form-control [nzSpan]="12" [nzErrorTip]="'validation.required' | i18n">
          <input
            [(colorPicker)]="tag.color"
            cpFallbackColor="#64d3ca"
            cpOutputFormat="hex"
            [style.background]="tag.color"
            required
            style="width: 100%"
            class="br-4"
            name="color"
            id="color"
          />
        </nz-form-control>
      </nz-form-item>
      <nz-form-item>
        <nz-form-label [nzSpan]="7" nzFor="description">{{ 'tag.description' | i18n }}</nz-form-label>
        <nz-form-control [nzSpan]="12" [nzErrorTip]="'validation.required' | i18n">
          <input [(ngModel)]="tag.description" nz-input name="description" type="text" id="description" />
        </nz-form-control>
      </nz-form-item>
      <nz-form-item *ngIf="tag.name != undefined">
        <nz-form-label [nzSpan]="7" nzFor="color">{{ 'tag.display' | i18n }}</nz-form-label>
        <nz-form-control [nzSpan]="12" [nzErrorTip]="'validation.required' | i18n">
          <nz-tag *ngIf="tag.tagValue == undefined || tag.tagValue.trim() == ''" [nzColor]="tag.color">{{ tag.name }}</nz-tag>
          <nz-tag *ngIf="tag.tagValue != undefined && tag.tagValue.trim() != ''" [nzColor]="tag.color">{{
            tag.name + ':' + tag.tagValue
          }}</nz-tag>
        </nz-form-control>
      </nz-form-item>
    </form>
  </div>
</nz-modal>
